package edu.xust.web.controller;

import edu.xust.config.*;
import edu.xust.dto.BaseDto;
import edu.xust.dto.MessageEnum;
import edu.xust.entity.Admin;
import edu.xust.page.PageInfo;
import edu.xust.service.AdminService;
import java.util.List;

/**
 * @auther: zhangxin
 * @Description:
 */
@Controller("/administratorsController")
public class AdministratorsController {

    @Resource
    AdminService adminService;

    @RequestMapping("/getAdmins")
    public BaseDto getAllAdmin(){

        BaseDto baseDto = new BaseDto(MessageEnum.FINDALLADMINSUCCESS);
        return baseDto;
    }

    @RequestMapping("/getAdminById/{adId}")
    @ResponseBody
    public BaseDto getAdminById(String adId){
        Admin admin = adminService.getAdminById(Integer.parseInt(adId));
        System.out.println(admin);
        BaseDto baseDto = new BaseDto(MessageEnum.FINDALLADMINSUCCESS,admin);
        System.out.println(baseDto.getT()+"...");
        return baseDto;
    }

    @RequestMapping("/addAdmin")
    @ResponseBody
    public BaseDto addAdmin(@RequestBody Admin admin){
       System.out.println(admin.toString());
       if(admin.getAdname().equals("")){
           return new BaseDto(MessageEnum.ADDADMINFAIL,admin);
       }
       if(admin.getAdsex().equals("")){
           return new BaseDto(MessageEnum.ADDADMINFAIL,admin);
       }
       if(!admin.getAdpassword().equals(admin.getRepassword())){
           return new BaseDto(MessageEnum.ADDADMINFAIL,admin);
       }
       if(adminService.addAdmin(admin) > 0){
           return new BaseDto(MessageEnum.ADDADMINSUCCESS,admin);
       }
        return new BaseDto(MessageEnum.ADDADMINFAIL,admin);
    }

    @RequestMapping("/editAdmin")
    @ResponseBody
    public BaseDto editAdmin(@RequestBody Admin admin){
        System.out.println(admin.toString());
        if(admin.getAdname().equals("")){
            return new BaseDto(MessageEnum.ADDADMINFAIL,admin);
        }
        if(admin.getAdsex().equals("")){
            return new BaseDto(MessageEnum.ADDADMINFAIL,admin);
        }
        if(!admin.getAdpassword().equals(admin.getRepassword())){
            return new BaseDto(MessageEnum.ADDADMINFAIL,admin);
        }
        if(adminService.editAdmin(admin) > 0){
            return new BaseDto(MessageEnum.ADDADMINSUCCESS,admin);
        }
        return new BaseDto(MessageEnum.ADDADMINFAIL,admin);
    }


    @RequestMapping("/getAdminsByPage")
    @ResponseBody
    public BaseDto getAdminsByPage(@RequestBody PageInfo pageInfo) throws Exception {
        List<Admin> admins = adminService.getAdminByPage(pageInfo);
        BaseDto baseDto = new BaseDto(MessageEnum.ADDADMINSUCCESS,admins);
        baseDto.setObj(pageInfo);
        return baseDto;
    }

    @RequestMapping("/deleteAdminById/{adid}")
    @ResponseBody
    public BaseDto deleteAdminsById(String adid) throws Exception {

        if(adminService.deleteAdminById(Integer.parseInt(adid)) > 0){
            return new BaseDto(MessageEnum.DELETEADMINSUCCESS);
        }
        return new BaseDto(MessageEnum.DELETEADMINSUCCESS);
    }





}
